<?php
/**
 * form控制器
 *
 * @package     Include  (controller)
 * @author      hary <hary@taomee.com>
 * @version     $ID 2010-3-31 $
 */
class Admin_Analizy extends Controller
{
	private $result;

	/**
	 * 构造函数
	 */
	function __construct()
	{
		parent::__construct();

		$this->loadModel('form_model');

		$this->loadModel('response_model');

		$this->loadClass('Mcache');

		$this->tpl->template_dir = SYS_PATH  . 'template/admin/form/';
	}

	/**
	 * 默认执行方法
	 *
	 * @return
	 */
	function index()
	{
		$this->getForms();
	}

	/**
	 * 获取表单列表
	 * @return response
	 */
	function analizy(){
		
		$formKey  = $this->in_var['key'];
		
		$data = $this->Mcache->getCache('survey',$formKey);
		
		if(empty($data))
		{
			$ana_ = $this->response_model->analizyResponse($formKey);
			$this->Mcache->setCache('survey',$formKey,$ana_);
		}
		else
		{
			$ana_ = $data;
		}

		$this->result['info'] = $ana_;

		$this->tpl->assign("result",$this->result);

		$this->tpl->display('ganalizy.html');
	}

	/**
	 * 获取文本反馈信息
	 * @return unknown_type
	 */
	function getReply(){
		
		$key 	 = $this->in_var['key'];
		$entryId = intval($this->in_var['entryid']);
		$type    = $this->in_var['type'];

		$entry = $this->form_model->getEntryItemByFormEntryId($key,$entryId);
		$title = $entry['e_question_title'];

		header("Content-type:application/vnd.ms-excel;charset=utf-8");
		header("Content-Disposition:attachment;filename=$title.xls");

		if($type=="1"||$type=="2")
		{
			$res 			      = $this->response_model->getResponseItemsByFormEntryId($key,$entryId);
			$this->result['list'] = $res;
			$this->result['Question'] = $title;
			
			$this->tpl->assign('result',$this->result);
			$this->tpl->display('excel_txt.html');
		}
		else
		{
			$info = $this->Mcache->getCache('survey',$key);
			$entry = null;
			
			foreach($info['Entries'] as $item)
			{
				if($item['EntryId']==$entryId)
				{
					$entry = $item;
					break;
				}
			}
			
			$this->tpl->assign('entry',$entry);
			$this->tpl->display('excel_list.html');
		}

	}

	/**
	 * 清空数据
	 * @return max
	 */
	function cleardata(){

		$key = $this->in_var['key'];

		$this->Mcache->clearCache("survey",$key);

		echo $this->response_model->clearData($key);
	}

	/**
	 * 清空服务器缓存
	 * @return max
	 */
	function clearMemecache(){
		
		$key = $this->in_var['key'];
		
		$this->Mcache->clearCache("survey",$key);
		
		echo true;
	}

}